#!/usr/bin/env python3
import shutil
import os


def print_list_tree(ls):
    print_tree_prefix(ls)


def print_string(string: str, prefix):
    if '\n' not in string:
        print(string)
        return
    #os_size = tuple(int(os.getenv(*v)) for v in (('COLUMNS', '1000'), ('LINES', '24')))
    width = shutil.get_terminal_size((1000,24)).columns - len(prefix)

    def print_split_longline(s):
        print(s[0:width])
        for i in range(width, len(s), width):
            print(prefix, s[i:i+width])
    lines = string.splitlines()
    for i, line in enumerate(lines):
        if i != 0:
            print(prefix, end='')
        print_split_longline(line)
        #print(line)


def print_tree_prefix(xs, prefix=''):
    if isinstance(xs, str):
        return print_string(xs, prefix)
    if not isinstance(xs, list):
        print('{}'.format(repr(xs)))
        return

    length = len(xs)

    #if length == 0:
    #    print('[]')
    #    return

    #print('┐')
    #print('╮')
    print('╖')

    for i, x in enumerate(xs):
        print(prefix, end='')
        if i == length - 1:
            print('╰───', end='')
            print_tree_prefix(x, prefix + '    ')
        # elif i == 0:
        #     print('┌───', end='')
        #     print_tree_prefix(x, prefix + '│   ')
        else:
            print('├───', end='')
            print_tree_prefix(x, prefix + '│   ')


if __name__ == '__main__':
    ls = [[1,2,3],4,[5,[6,7,9],8],[[[]]]]
    print_tree_prefix(ls, '')
